﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Model.SiteAdmin;
using Jzy.Core.Utility;
using Jzy.Core.Common.Basic;
using Jzy.BLL.SiteAdmin;
using Jzy.Core.Common.Web;

namespace Jzy.Web.SiteAdmin.Menu
{
    public partial class List2 : PageList
    {
        private object _menuID;
        private object MenuID
        {
            get
            {
                if (_menuID == null)
                {
                    _menuID = _menu.GetIDByCode(Request["MenuCode"]);
                }
                return _menuID;
            }
        }
        private MenuBO _menu = new MenuBO();
        private string strOrders;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 80);
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            IEnumerable<MenuInfo> list = _menu.GetList(int.Parse(MenuID.ToString()));            
            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID", "UpperID" };
            lv.DataBind();

        }

        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "栏目");

            string url = GlobalVar.WebCurrPath + "Edit2.aspx?UpperID=" + StrUtil.EncryptUrl(MenuID);
            JSUtil.ShowForm(lnkAdd, "增加栏目", null, url, 300, 191);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            List<int> ids = StrUtil.SplitToInt(strSelectIDs);

            _menu.Delete(ids.ToArray());
            BindView();
        }

        protected void btnSaveOrders_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            _menu.SaveOrders(ids.ToArray(), orders.ToArray());
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 300, 191, "Edit2.aspx");

            }
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
    }
}
